<?php

namespace api\modules\warranty\controllers;

use Yii;

/**
 * DefaultController implements the CRUD actions for Config model.
 */
class DefaultController extends \api\controllers\CurdController
{
    public function init(){
        parent::init();
        $this->modelServie = \api\service\warranty\LotsrService::instance();
    }

    public function actionUpload(){
        if(Yii::$app->request->isPost){
            $model = new \api\modules\warranty\models\LotsrUpload();            
            $model->file = \yii\web\UploadedFile::getInstanceByName('file');
            if($model->validate()){
                try {
                    $message = $this->modelServie->upload($model);
                    return ['status'=>1, 'message'=>$message];
                } catch (\Throwable $th) {
                    return ['status'=>0, 'message'=>$th->getMessage()];
                }                
            }
            return ['status'=>0, 'message'=>\common\utils\FunUtil::currentFistErrors($model->getFirstErrors())];
        }
        return ['message'=>'Request method error'];
    }

   
    public function actionExport(){
        return $this->modelServie->exportXls(Yii::$app->request->post());
    }

    public function actionDropdonwList(){
        $types = Yii::$app->request->get('type');
        $types = explode(',', $types);
        $list = [];
        if(!empty($types)){
            foreach ($types as $val) {
                if($val=='tnc'){
                    $data = $this->modelServie->getCategorys(['type'=>$val]);
                    $list[$val] = $this->modelServie->buildDropDownList($data, 0, $val);
                }else{
                    $data = $this->modelServie->getCategorys(['type'=>$val]);
                    $list[$val] = $this->modelServie->buildDropDownList($data, 0);
                }
            }
        }
        return $list;
    }
}
